Grails : DomainClass Dynamic Methods
This page last changed on Mar 15, 2006 by [email protected].
Domain Class Dynamic Methods & PropertiesPropertieserrorsDescriptionA list of errors from the last call to the "validate" or "save" method Exampledef b = new Book(title:"The Shining") if(!b.validate()) { b.errors.each { println it } } constraintsDescriptionA list of org.codehaus.groovy.grails.validation.ConstrainedProperty instances applied against the domain class by the constraints property (see Validation) Exampledef b = new Book(title:"The Shining") b.constraints.each { println it.name println it.maxLength } propertiesDescriptionAllows access to the domain class properties as a map and perform types conversion when set allowing properties to be set from request parameters for example. Exampledef b = new Book(title:"The Shining") b.properties = this.params MethodsdeleteDescriptionDeletes a domain class instance from the database Exampledef b = Book.get(1) b.delete() hasErrorsDescriptionTrue if the domain class instance has errors following a call to "validate" or "save" Exampledef b = new Book(title:"The Shining") b.validate() if(b.hasErrors()) { b.errors.each { println it } } identDescriptionReturns the value of the identity property of the domain class regardless of the name of the identity property itself Exampledef b = new Book(title:"The Shining") b.save() println b.ident() refreshDescriptionRefreshes a domain classes state from the database Exampledef b = Book.get(1) b.refresh() saveDescriptionSaves a domain class instance to the database cascading updates to any child instances if required. Returns false if validation failed and the instance was not saved Parameters
Exampledef b = new Book(title:"The Shining") if( !b.save() ) { b.errors.each { println it } } validateDescriptionValidates a domain class against the applied constraints (see Validation) Exampledef b = new Book(title:"The Shining") if( !b.validate() ) { b.errors.each { println it } } Static MethodscountDescriptionCounts the number of instances in the database and returns the result ParametersNone Exampledef noOfBooks = Book.count() createCriteriaDescriptionCreates a grails.orm.HibernateCriteriaBuilder instance for the domain class. (see Builders) Exampledef c = Account.createCriteria() def results = c { like("holderFirstName", "Fred%") and { between("balance", 500, 1000) eq("branch", "London") } maxResults(10) order("holderLastName", "desc") } existsDescriptionChecks whether an instance exists for the specified id and returns true if it does Parameters
Exampleif(Account.exists(1)) { // do something } findDescriptionFinds and returns the first result for the given query or null if no instance was found Parameters
ExampleBook.find("from Book as b where b.author='Dan Brown'") // Dan brown's first book Book.find("from Book as b where b.author=?",['Dan Brown']) // with a named parameter def b = new Book(author:"Dan Brown") Book.find(b) // query by example findAllDescriptionFinds all of the domain class instances for the specified query Parameters
ExamplesBook.findAll() // everything Book.findAll("from Book as b where b.author='Dan Brown'",10) // The first 10 books from Dan Brown Book.findAll("from Book as b where b.author=?",['Dan Brown'],10) // with a named parameter def b = new Book(author:"Dan Brown") Book.findAll(b) // query by example findByDescriptionDynamic method that uses the properties of the domain class to allow the creation of Grails query method expressions that return the first result of the query ExamplesBook Domain Class class Book { @Property Long id @Property Long version @Property String title @Property Date releaseDate @Property String author } Examples def b = Book.findByTitle("The Shining") b = Book.findByTitleAndAuthor("The Sum of All Fears", "Tom Clancy") b = Book.findByReleaseDateBetween(firstDate, new Date()) b = Book.findByReleaseDateGreaterThanOrEqual(firstDate) b = Book.findByTitleLike("%Hobbit%") b = Book.findByTitleNotEqual("Harry Potter") b = Book.findByReleaseDateIsNull() b = Book.findByReleaseDateIsNotNull() findAllByDescriptionDynamic method that uses the properties of the domain class to allow the creation of Grails query method expressions that return all instances of the domain class ExamplesBook Domain Class class Book { @Property Long id @Property Long version @Property String title @Property Date releaseDate @Property String author } Examples def results = Book.findAllByTitle("The Shining", [max:10, sort:"title", order:"desc", offset:100] ) results = Book.findAllByTitleAndAuthor("The Sum of All Fears", "Tom Clancy") results = Book.findAllByReleaseDateBetween(firstDate, new Date()) results = Book.findAllByReleaseDateGreaterThanOrEqual(firstDate) results = Book.findAllByTitleLike("%Hobbit%") results = Book.findAllByTitleNotEqual("Harry Potter") results = Book.findAllByReleaseDateIsNull() results = Book.findAllByReleaseDateIsNotNull() findWhereDescriptionUses named arguments that match the property names of the domain class to produce a query that returns the first result. ExamplesBook Domain Class class Book { @Property Long id @Property Long version @Property String title @Property Date releaseDate @Property String author } Examples def b = Book.findWhere(title:"The Shining", author:"Stephen King") getDescriptionRetrieves an instance of the domain class for the specified id, otherwise returns null Examplesdef b = Book.get(1) listDescriptionLists all of the instances of the domain class. Parameters
Examplesdef results = Book.list() // everything def results = Book.list(max:10) // 10 results def results = Book.list(max:10, offset:100) // 10 results, offset by 100 def results = Book.list(max:10, offset:100, sort:"title", order:"desc") // 10 results, offset by 100, orderd by title in descending order listOrderByDescriptionLists all of the instances of the domain class ordered by the property in the method expression Parameters
Examplesdef results = Book.listOrderByAuthor() // everything def results = Book.listOrderByTitle(max:10) // 10 results def results = Book.listOrderByTitle(max:10, offset:100, order:"desc") // 10 results, offset from 100 |
Document generated by Confluence on Mar 29, 2006 08:46 |